package com.chainyoung.component_tmperson.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.entity.OwnWalletDebitItem;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.activity.debit.ConfirmRepayDebitActivity;
import com.chainyoung.component_tmperson.ui.activity.debit.OwnWalletDebitDetailActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OwnWalletDebitAdapter extends BaseQuickAdapter<OwnWalletDebitItem, BaseViewHolder> {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private WalletDebit walletDebit;

    public OwnWalletDebitAdapter(@Nullable List<OwnWalletDebitItem> data, WalletDebit walletDebit) {
        super(R.layout.layout_own_wallet_debit_item, data);
        this.walletDebit = walletDebit;
    }

    @Override
    protected void convert(BaseViewHolder helper, final OwnWalletDebitItem item) {
        helper.setText(R.id.tv_amount, StringUtil.subZeroAndDot(item.getPrincipal()) + item.getCoin().toUpperCase());
        helper.setText(R.id.tv_time, "借币时间：" + sdf.format(new Date(item.getAddtime() * 1000)));
        helper.setText(R.id.tv_percent, item.getRepaid_period() / item.getPeriod() * 100 + "%");

        helper.getView(R.id.view).setVisibility(item.getAudit_status() == 1 ? (item.getRepay_complete()==1 ? View.GONE : View.VISIBLE) : View.GONE);
        helper.getView(R.id.tv_repay).setVisibility(item.getAudit_status() == 1 ? (item.getRepay_complete()==1 ? View.GONE : View.VISIBLE) : View.GONE);
        helper.getView(R.id.tv_percent).setVisibility(item.getAudit_status() == 1 ? (item.getRepay_complete()==1 ? View.GONE : View.VISIBLE) : View.GONE);
        helper.getView(R.id.tv_status).setBackgroundResource(item.getAudit_status()==1? R.drawable.shape_debit_repay_mark:
        item.getAudit_status()==0?R.drawable.shape_debit_repay_mark_gray:R.drawable.shape_debit_repay_mark_red);

        switch (item.getAudit_status()) {
            case 0://审核中
                helper.setText(R.id.tv_status, "审核中");
                break;
            case 1://审核成功
                helper.setText(R.id.tv_status, item.getRepay_complete()==1?"已还清":"已还币");
                break;
            case 2://审核失败
                helper.setText(R.id.tv_status, "审核失败");
                break;
        }

        helper.getView(R.id.rl_debit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getAudit_status() == 1) {
                    Intent intent = new Intent(mContext, OwnWalletDebitDetailActivity.class);
                    intent.putExtra("debitId", item.getId());
                    intent.putExtra("isCompleteRepayed",item.getRepay_complete()==1);
                    intent.putExtra("walletDebit", walletDebit);
                    mContext.startActivity(intent);
                }
            }
        });

        helper.getView(R.id.tv_repay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ConfirmRepayDebitActivity.class);
                intent.putExtra("debitId", item.getId());
                intent.putExtra("walletDebit", walletDebit);
                mContext.startActivity(intent);
            }
        });

    }


}
